import java.util.PriorityQueue;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: hp
 * Date: 2024-07-31
 * Time: 20:55
 */

class MyTimerTask implements Comparable<MyTimerTask>{
    private long time;
    private Runnable runnable;

    public MyTimerTask(Runnable runnable,long time) {
        this.runnable = runnable;
        this.time = System.currentTimeMillis() + time;
    }

    public long getTime() {
        return this.time;
    }

    @Override
    public int compareTo(MyTimerTask o) {
        return (int) (this.time - o.time);
    }

    public void run() {
        runnable.run();
    }
}

class MyTimer {

    private static Object locker = new Object();

    private static PriorityQueue<MyTimerTask> queue = new PriorityQueue<>();
    public MyTimer() {
        Thread t = new Thread(() -> {
            synchronized(locker) {
                while(true) {
                    while(queue.isEmpty()) {
                        try {
                            locker.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    MyTimerTask current = queue.peek();
                    if(System.currentTimeMillis() >= current.getTime()) {
                        current.run();
                        queue.poll();
                    }else {
                        try {
                            locker.wait(current.getTime() - System.currentTimeMillis());
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }
            }
        });
        t.start();
    }

    public void schedule(Runnable runnable, long delay){
        synchronized(locker) {
            MyTimerTask myTimerTask1 = new MyTimerTask(runnable,delay);
            queue.offer(myTimerTask1);
            locker.notify();
        }
    }
}




public class Dmoe {
    public static void main(String[] args) {
        Timer timer = new Timer();
        MyTimer myTimer = new MyTimer();
        /*timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println(3000);
            }
        }, 3000);

        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println(2000);
            }
        }, 2000);

        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println(1000);
            }
        }, 1000);

        System.out.println("线程开始");*/

        myTimer.schedule(() -> {
            System.out.println(3000);
        },3000);
        myTimer.schedule(() -> {
            System.out.println(2000);
        },2000);
        myTimer.schedule(() -> {
            System.out.println(1000);
        },1000);

        System.out.println("线程开始");
    }
}
